import request from '@/utils/request'

// 获取分类列表
export function getCategories(params) {
  let url = '/categories'
  if (params) {
    let str = '?'
    for (const key in params) {
      str += `${key}=${params[key]}&`
    }
    str = str.substring(0, str.length - 1)

    url += str
  }

  return request({ url })
}

// 新增分类
export function addCategory(data) {
  return request({ url: '/categories', method: 'post', data })
}

// 获取分类数量
export function getCategoriesTotal(params) {
  return request({ url: '/categories/count', params })
}

// 删除分类
export function deleteCategory(id) {
  return request({ url: `/categories/${id}`, method: 'delete' })
}

// 编辑分类
export function editCategory(data) {
  return request({ url: `/categories/${data.id}`, data, method: 'put' })
}

// 获取指定分类
export function getCategoryById(id) {
  return request({ url: `/categories/${id}` })
}
